package com.bayss.web.mock.response;


/**
 * 统一HTTP响应格式，通过_200、_900等静态方法快速构造响应报文
 * <p/>
 * Created by px on 15/4/30.
 */
public class MockResp {

    // 为200时表示成功，否则为失败错误码
    private int code;
    // 错误信息
    private String message;
    // 业务数据体
    private Object data;

    private static final MockResp success = new MockResp(MockAppCode._200, null);
    private static final MockResp error = new MockResp(MockAppCode._900, null);


    public MockResp(MockAppCode code, Object data) {
        this.data = data;
        this.code = code.getCode();
        this.message = code.getMsg();
    }

    public MockResp(MockAppCode code, Object[] args) {
        this.data = null;
        this.code = code.getCode();
        this.message = String.format(code.getMsg(), args);
    }

    public static MockResp success() {
        return success;
    }

    public static MockResp success(Object data) {
        return new MockResp(MockAppCode._200, data);
    }

    public static MockResp error() {
        return error;
    }

    public static MockResp error(MockAppCode code) {
        return new MockResp(code, null);
    }

    public static MockResp error(MockAppCode code, Object[] args) {
        return new MockResp(code, args);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
